package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.ChangeVo;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-02
 */
@RestController
@RequestMapping("/educenter/member")

public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R login(@RequestBody UcenterMember ucenterMember){
        //返回token值，使用jwt生成
        String token =memberService.login(ucenterMember);
        return R.ok().data("token", token);

    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody  RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }
    //更改密码
    @ApiOperation(value = "更改密码")
    @PostMapping("change")
    public R changePassword(@RequestBody ChangeVo changeVo) {
        memberService.changePasswd(changeVo);
        return R.ok().message("修改密码成功");
    }

    //根据token值获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }
    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        UcenterMember member = memberService.getById(id);
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }
    //根据用户id获取用户信息
    @ApiOperation(value = "根据用户id获取用户信息 个人中心用")
    @PostMapping("getUserInfo/{id}")
    public R getUserInfo(@PathVariable String id) {
        //  public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        //       return ucenterMemberOrder;
        return R.ok().data("memberInfo",ucenterMemberOrder);
    }
    //用户信息修改功能
    @ApiOperation(value = "用户信息修改")
    @PostMapping("updateMember")
    public R updateMember(@RequestBody UcenterMember ucenterMember) {
        boolean flag = memberService.updateById(ucenterMember);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
       Integer count= memberService.countRegister(day);
       return R.ok().data("countRegister",count);
    }
}

